﻿
function stringReplaceAll(AFindText, ARepText) {
    var raRegExp = new RegExp(AFindText.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g, "\\$1"), "ig");
    return this.replace(raRegExp, ARepText);
}
String.prototype.replaceAll = stringReplaceAll;

/** 
* 时间对象的格式化; 
*/
Date.prototype.format = function (format) {
    /* 
    * eg:format="yyyy-MM-dd hh:mm:ss"; 
    */
    var o = {
        "M+": this.getMonth() + 1, // month  
        "d+": this.getDate(), // day  
        "h+": this.getHours(), // hour  
        "m+": this.getMinutes(), // minute  
        "s+": this.getSeconds(), // second  
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter  
        "S": this.getMilliseconds()
        // millisecond  
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4
                        - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                            ? o[k]
                            : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}
$.format = function (formatString) {
    if (arguments.length > 1) {
        for (var i = 1; i < arguments.length; i++) {
            var raRegExp = new RegExp('\\{' + (i - 1) + '\\}', "g");
            formatString = formatString.replace(raRegExp, arguments[i]);
        }
        return formatString;
    }
    return formatString;

};
$.escapeHTML = function (b) {
    var a = $("<div />");
    a.text(b);
    return a.html()
};
//$.messager.defaults.ok = "確定";
//$.messager.defaults.cancel = "取消";
//$.fn.pagination.defaults.displayMsg = "共{total}条";
//$.fn.pagination.defaults.beforePageText ="第";
//$.fn.pagination.defaults.afterPageText = "页/共{pages}页";
//$.fn.datagrid.defaults.loadMsg= "正在加载数据...";

//修改默认filter,处理数据加载异常
$.fn.datagrid.defaults.loadFilter = function (data) {
    if (typeof data.total == "number" && !isNaN(data.total)) {
        return data;
    } else {
        if (typeof data.length == "number" && typeof data.splice == "function") {
            return { total: data.length, rows: data };
        }
        else {
            if (data && data.Success == false) {
                BasicJs.showMessage(data.Message, data.Success);
                return { total: 0, rows: [] };
            }
            else {
                BasicJs.showMessage("加载数据出错！", false);
                return { total: 0, rows: [] };
            }
        }
    }
};
var BasicJs = {

    bindEvent: function () {

        $(document).keydown(function (e) {
            var target = e.target;
            var tag = e.target.tagName.toUpperCase();
            if (e.keyCode == 8) {
                if ((tag == 'INPUT' && !$(target).attr("readonly")) || (tag == 'TEXTAREA' && !$(target).attr("readonly"))) {
                    if ((target.type.toUpperCase() == "RADIO") || (target.type.toUpperCase() == "CHECKBOX")) {
                        return false;
                    } else {
                        return true;
                    }
                } else {
                    return false;
                }
            }
        });
    },
    autoDialogSize: function (dlgH, dlgW) {
        var winH = $(window).height();
        var winW = $(window).width();
        var dlgleft = (winW - dlgW) / 2;
        var dlgtop = (winH - dlgH) / 2;


        if (winH < dlgH) {
            dlgtop = 0;
            dlgH = winH;
        }
        if (winW < dlgW) {
            dlgleft = 0;
            dlgW = winW;
        }
        if (dlgleft < 0)
            dlgleft = 0;
        if (dlgtop < 0)
            dlgtop = 0;
        if (dlgW < 50)
            dlgW = 50;
        if (dlgH < 40)
            dlgH = 40;
        var parms = {
            Left: dlgleft,
            Top: dlgtop,
            Width: dlgW,
            Height: dlgH
        };
        return parms;
    },
    showMessage: function (msg, success, callback) {
        top.main.showMessage(msg, success, callback);
    },
    initGridHeidht: function () {
        //表格全屏高度
        var contentH = $(window).innerHeight() - $("#toolbar").outerHeight() - $(".panel-header").outerHeight() - 10;
        if (contentH < 100) contentH = 100;
        $(".datagrid-wrap").height(contentH);
        $(".datagrid-wrap").each(function () {
            var gridbodyH = contentH - $(this).find(".datagrid-pager").outerHeight();
            $(this).find(".datagrid-view").height(gridbodyH);
            $(this).find(".datagrid-body").height(gridbodyH - $(this).find(".datagrid-header").outerHeight());

        });
    },
    getGridHeight: function () {
        var contentH = $(window).height() - $("#toolbar").outerHeight() - $(".panel-header").outerHeight() - 10;
        if (contentH < 100) contentH = 100;
        return contentH;
    },
    valField: function (id, len, name, require) {
        var value = $.trim($("#" + id).val());
        if (len == null) {
            if (require && value == "") { return $.format("{0}为必须项！", name); }
            else return "";

        }
        if (require && (value == "" || value.length > len)) { return $.format("{0}为必须项，最长{1}字符！", name, len); }
        else if (value.length > len) { return $.format("{0}最长{1}字符！", name, len); }
        return "";
    },
    getCenter: function (id) {
        var nDivLeft = $(window.top).width();
        var nDivTop = $(window.top).height();
        return { "left": (nDivLeft - parseInt($(id).width())) / 2, "top": (nDivTop - parseInt($(id).height())) / 2 };
    },
    getIFrameCenter: function (id) {
        var nDivLeft = $(window).width();
        var nDivTop = $(window).height();
        var frameTop = (nDivTop - parseInt($(id).height())) / 2 - 10; //-10稍微靠上点
        if (frameTop < 0) frameTop = 0;
        return { "left": (nDivLeft - parseInt($(id).width())) / 2, "top": frameTop };
    },
    dialog: function (width, height, title, url, callback, param) {
        var wHeight = $(window.top).height();
        var left = ($(window.top).width() - width) / 2;
        var top = (wHeight - height) / 2;
        if (left < 0) left = 0;
        if (top < 0) top = 0;
        //弹窗高度不能超出窗体高度
        if (height > wHeight) height = wHeight;
        window.top.main.dialog(left, top, width, height, title, url, callback, param);
    },

    authorize: function (id, funKey, action) {
        var isAdmin = window.top.cacheData.IsAdmin;
        if (isAdmin) {
            $(id).show();
            return;
        }
        var authData = window.top.cacheData.FActions;
        var authLst = $.grep(authData, function (item) { return item.FUNC_KEY == funKey && item.ACTION_ID == action; });
        if (authLst.length > 0) $(id).show();
        else $(id).hide();
    },
    isAuth: function (funKey, action) {
        var isAdmin = window.top.cacheData.IsAdmin;
        if (isAdmin) {
            return true;
        }
        var authData = window.top.cacheData.FActions;
        var authLst = $.grep(authData, function (item) { return item.FUNC_KEY == funKey && item.ACTION_ID == action; });
        if (authLst.length > 0) return true;
        else return false;

    },
    //获取弹窗参数
    getParam: function () {
        return window.top.main.editRow;
    },
    getDate: function (text, format) {
        if (!text || $.trim(text) == "") return null;
        else return (new Date(text.replace("-", "/").replace("-", "/"))).format(format);
    },
    getBoolenState: function (value) {

        if (value == "Y")
            return "是"
        else
            return "否";

    },
    //格式化委托单状态 
    getConsignStatus: function (i) {
        switch (i) {
            case 1:
                return "待审核";
                break;
            case 2:
                return "待排程";
                break;
            case 3:
                return "排程中";
                break;
            case 4:
                return "待审核排程";
                break;
            case 5:
                return "待组长重排";
                break;
            case 6:
                return "待委托方确认排程";
                break;
            case 7:
                return "生效待实验";
                break;
            case 8:
                return "待TE重排";
                break;
            case 9:
                return "实验中";
                break;
            case 10:
                return "待报告审核";
                break;
            case 11:
                return "待报告更正";
                break;
            case 12:
                return "待出报告";
                break;
            case 13:
                return "待委托方确认报告";
                break;
            case 14:
                return "待报告调整";
                break;
            case 15:
                return "结案";
                break;
            case 16:
                return "驳回";
                break;
        }

    },
    //状态下拉数据源
    getConsignStatusArr: function () {
        return [{ id: 1, text: "待审核" }, { id: 2, text: "待排程" }, { id: 3, text: "排程中" }, { id: 4, text: "待审核排程" }, { id: 5, text: "待组长重排" }, { id: 6, text: "待委托方确认排程" },
        { id: 7, text: "生效待实验" }, { id: 8, text: "待TE重排" }, { id: 9, text: "实验中" }, { id: 10, text: "待报告审核" }, { id: 11, text: "待报告更正" },
        { id: 12, text: "待出报告" }, { id: 13, text: "待委托方确认报告" }, { id: 14, text: "待报告调整" }, { id: 15, text: "结案" }, { id: 16, text: "驳回"}];
    },
    //格式化检测项目状态 
    getTIStatus: function (i) {
        switch (i) {
            case 1:
                return "待排程";
                break;
            case 2:
                return "待排程确认";
                break;
            case 3:
                return "待重排";
                break;
            case 4:
                return "待领样";
                break;
            case 5:
                return "实验中";
                break;
            case 6:
                return "待更换样品";
                break;
            case 7:
                return "待确认";
                break;
            case 8:
                return "待报告调整";
                break;
            case 9:
                return "完成";
                break;
        }

    },
    //检测项目状态下拉数据源
    getTIStatusArr: function () {
        return [{ id: 1, text: "待排程" }, { id: 2, text: "待排程确认" }, { id: 3, text: "待重排" }, { id: 4, text: "待领样" }, { id: 5, text: "实验中" },
        { id: 6, text: "待更换样品" }, { id: 7, text: "待确认" }, { id: 8, text: "待报告调整" }, { id: 9, text: "完成"}];
    },
    //格式化结果 
    getResult: function (i) {
        switch (parseInt(i)) {
            case 1:
                return "PASS";
                break;
            case 2:
                return "FAIL";
                break;
            case 3:
                return "Reference";
                break;
        }

    },
    getScheduleTypeData: function (fackeys, fac_data) {
        var sData = this.getCKItems("ScheduleType", fackeys);
        var dataSource = [];

        $.each(sData, function (index, row) {
            var facRow = $.grep(fac_data, function (item) {
                return item.FAC_KEY == row.FAC_KEY;
            });
            var facName = "";
            if (facRow.length > 0) {
                facName = facRow[0].FACTORY_NAME + "-";
            }
            var parms = { SYS_CHECKBOXLIST_KEY: row.SYS_CHECKBOXLIST_KEY,
                CHECKBOXLIST_NAME: facName + row.CHECKBOXLIST_NAME
            }
            dataSource.push(parms);
        });
        return dataSource.sort(function (a, b)
        { return (a.CHECKBOXLIST_NAME == b.CHECKBOXLIST_NAME) ? 0 : ((a.CHECKBOXLIST_NAME > b.CHECKBOXLIST_NAME) ? 1 : -1) });
    },
    //读取下拉框可选项 : 参数facKeys是厂别数组 
    // 项目阶段 typeCode="ProjectStage" ; 样品类别 typeCode="Category" ;   功能测试冶具状态 typeCode="FuncTestMacStatus" ; 
    // 样品处置 typeCode="SampleManage" ; 报告类型 typeCode="ReportType" ; 排程类别 typeCode="ScheduleType" 
    getCKItems: function (typeCode, facKeys) {
        var Items = window.top.cacheData.CheckBoxItems;
        return $.grep(Items, function (item) { return item.CHECKBOX_TYPE_CODE == typeCode && $.inArray(item.FAC_KEY.toString(), facKeys) >= 0; });
    },
    //根据选项的KEY读取下拉选项内容
    getCKItemValue: function (itemKey) {
        var Items = window.top.cacheData.CheckBoxItems;
        var findItems = $.grep(Items, function (item) { return item.SYS_CHECKBOXLIST_KEY == itemKey; });
        if (findItems.length > 0) return findItems[0].CHECKBOXLIST_NAME;
        else return "";

    },
    formatTitle: function (value) {
        if (value == "") return value;
        else return $.format("<span title='{0}'>{0}</span>", value);
    }


}